﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Benefit.Models.Sys;
using Benefit.Models.Year;
namespace Benefit.Service.Year
{
    /// <summary>
    /// 小组年均值
    /// </summary>
    public class ServiceGroupAvgYear : Benefit.Interface.Year.IGroupAvgYear
    {
        Benefit.DB.DBManager db = null;
        public ServiceGroupAvgYear(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 删除当年所有记录
        /// </summary>
        /// <param name="weekId"></param>
        public void Delete(int year)
        {
            var query = from t in db.GroupAvgYear where t.Year == year select t;
            foreach (Models.Year.GroupAvgYear c in query)
            {
                db.GroupAvgYear.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存年小组累计值
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitGroupAvgYear(int year)
        {
            List<Models.Sys.Groups> groups = db.Groups.ToList();
            foreach (Models.Sys.Groups g in groups)
            {
                GroupAvgYear day = new GroupAvgYear();
                day.GroupId = g.Id;
                day.Year = year;

                var queryExist = from t in db.GroupAvgYear where t.Year == year && t.GroupId == g.Id select t;
                if (queryExist.Count() > 0)
                {
                    day = queryExist.First();//如果有年均值，就赋值
                }

                var query = db.GroupAvgMonth.Where(a => a.Year == year).Where(a => a.GroupId == g.Id).ToList();


                day.BillCount = Math.Round(query.Average(a => a.BillCount), 2);
                day.DayCount = Math.Round(query.Average(a => a.DayCount), 2);
                day.Free = Math.Round(query.Average(a => a.Free), 2);
                day.LoseCount = Convert.ToInt32(query.Average(a => a.LoseCount));
                day.Profit = Math.Round(query.Average(a => a.Profit), 2);
                day.SumLost = Math.Round(query.Average(a => a.SumLost), 2);
                day.SumWin = Math.Round(query.Average(a => a.SumWin), 2);
                day.UsedMargin = Math.Round(query.Average(a => a.UsedMargin), 2);
                day.WinCount = Convert.ToInt32(query.Average(a => a.WinCount));
                day.AvgBillBenefit = Math.Round(query.Average(a => a.AvgBillBenefit), 2);
                day.AvgPostionTime = Math.Round(query.Average(a => a.AvgPostionTime), 2);
                day.MaxReturn = Math.Round(query.Average(a => a.MaxReturn), 2);
                day.OverStopLoss = Math.Round(query.Average(a => a.OverStopLoss), 2);
                day.PingJinPing = Convert.ToInt32(query.Average(a => a.PingJinPing));
                day.RaiseRate = Math.Round(query.Average(a => a.RaiseRate), 2);
                day.RiskCount = Convert.ToInt32(query.Average(a => a.RiskCount));
                day.Utilization = Math.Round(query.Average(a => a.Utilization), 2);
                day.WinRate = Math.Round(query.Average(a => a.WinRate), 2);
                day.Yield = Math.Round(query.Average(a => a.Yield), 2);
                if (queryExist.Count() == 0)
                {
                    db.GroupAvgYear.Add(day);
                }
            }
            db.SaveChanges();
        }

        public GroupAvgYear GetGroupAvgYear(int groupId, int year)
        {
            var query = db.GroupAvgYear.Where(a => a.GroupId == groupId).Where(a => a.Year == year);
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return null;
            }
        }
    }
}
